package com.geekcoding.geekoj.config;

import com.geekcoding.common.dto.ApiResponse;
import com.geekcoding.common.exceptions.GeekRuntimeException;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @author xiaobai
 * @since 2024/03/20 20:56
 **/

@ControllerAdvice
@ResponseBody
@Log4j2
public class GlobalExceptionHandler {

    @ExceptionHandler(GeekRuntimeException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ApiResponse exceptionHandle(GeekRuntimeException exception){
        log.error(exception);
        return ApiResponse.error(exception.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ApiResponse exceptionException(Exception ex){
        log.error(ex);
        return ApiResponse.error("系统异常");
    }
}
